package saer.send.now.channel;

import java.io.IOException;
import java.io.InputStream;

public class ChannelInputStream {
    private InputStream input;

    public ChannelInputStream(InputStream input) {
        this.input = input;
    }
    
    public int skippHeaders() throws IOException {
        int found = 0;
        int next = 13;
        int counter = 0;

        while (found != 4) {
            int data = this.input.read();
            if (data == -1 || counter > 500) {
                throw new RuntimeException("Failed to skipp headers");
            }

            counter++;
            if (data == next) {
                found++;
                next = (next == 10 ? 13 : 10);
            } else {
                next = 13;
                found = 0;
            }
        }
        
        return counter;
    }

    public int read(byte[] buffer, int length) throws IOException {
        return this.input.read(buffer, 0, length);
    }

    public void close() throws IOException {
        this.input.close();
    }
}
